package com.ostrich.cloud.ostrichGoodsService.app.ApiImp;

import cn.hutool.core.bean.BeanUtil;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.GrounpByDto;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.GrounpGoodsDto;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.CreateGrounpByParam;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.CreateGrounpParam;
import com.ostrich.cloud.ostrichGoodsService.app.component.Utlis;
import com.ostrich.cloud.ostrichGoodsService.app.createJob.XxlJobCreateParam;
import com.ostrich.cloud.ostrichGoodsService.app.createJob.XxlJobManager;
import com.ostrich.cloud.ostrichGoodsService.client.GrounpClient;
import com.ostrich.cloud.ostrichGoodsService.infra.model.GrounpGoods;
import com.ostrich.cloud.ostrichGoodsService.service.GrounpByService;
import com.ostrich.cloud.ostrichGoodsService.service.GrounpGoodsService;
import com.ostrich.cloud.ostrichGoodsService.service.param.AddGrounpByParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.AddGrounpParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class GrounpApiImp implements GrounpClient {

    @Resource
    private GrounpGoodsService grounpGoodsService;

    @Resource
    private GrounpByService grounpByService;

    @Resource
    private XxlJobManager xxlJobCreator;

    @Override
    public Result createGrounp(CreateGrounpParam param) {
        AddGrounpParam addGrounpParam = BeanUtil.copyProperties(param, AddGrounpParam.class);
        GrounpGoods grounpGoods = grounpGoodsService.addGrounpGoods(addGrounpParam);
        XxlJobCreateParam param1 = new XxlJobCreateParam();
        param1.setExecutorId(2);
        param1.setJobDesc("拼团时间到开启拼团商品任务");
        param1.setScheduleConf(Utlis.dateToCron(param.getStartTime()));
        param1.setExecutorHandler("startGrounpGoods");
        param1.setExecutorParam(grounpGoods.getGroundId()+"");
        int jobId = xxlJobCreator.createJob(param1);
        XxlJobCreateParam xxlJobCreateParam = new XxlJobCreateParam();
        xxlJobCreateParam.setExecutorId(2);
        xxlJobCreateParam.setJobDesc("结束拼团商品任务");
        xxlJobCreateParam.setExecutorHandler("endGrounpGoods");
        xxlJobCreateParam.setScheduleConf(Utlis.dateToCron(param.getEndTime()));
        xxlJobCreateParam.setExecutorParam(grounpGoods.getGroundId()+"");
        int job = xxlJobCreator.createJob(xxlJobCreateParam);
        return Result.ok();
    }

    @Override
    /**
     *
     */
    public Result createGrounpBy(CreateGrounpByParam param) {
        AddGrounpByParam addGrounpByParam = BeanUtil.copyProperties(param, AddGrounpByParam.class);
        addGrounpByParam.setUserId(Utlis.getParentId());
        grounpByService.addGrounpBy(addGrounpByParam);
        return Result.ok();
    }

    @Override
    public Result getGrounpBy(Integer groundGoodsId) {

        List<GrounpByDto> grounpByDtos = grounpByService.selectGrounpBy(groundGoodsId);


        return Result.ok(grounpByDtos);
    }

    @Override
    public Result getGrounp(Integer current,Integer size) {

        PageResult<GrounpGoodsDto> pageResult=grounpGoodsService.selectGrounpGoods(current,size);

        return Result.ok(pageResult);
    }
}
